<?php
session_start();
if ($_SESSION['user'] == "") {
    ?>
    <script>
        alert('当前未登录！');
        window.location.href = '../login.php';
    </script>
    <?php
}?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>还书模式</title>
    <link href="/css/index.css" rel="stylesheet" type="text/css" >
    <link href="/css/huanshu.css" rel="stylesheet" type="text/css" >
</head>
<body>
<div class="men">
    <div class="main_top">
        <?php include '../html/top.php'?>
    </div>
    <div class="main_center">
        <div class="main_center_left">
            <?php include '../html/left.php'?>
        </div>
        <div class="main_center_right">
            <form action="/action/huanshuAction.php" method="post">
                <div class="main">
                    <?php
                    //连接数据库
                    try {
                        $db = new PDO('mysql:dbname=books;host=127.0.0.1','root','123456');
                        $update= $db->prepare('update record set isreturnTime=:isreturnTime where bookid=:bookid and userid=:phone');
                        $data[':isreturnTime']=$_POST['isreturnTime'];
                        $data[':bookid']=$_POST['bookid'];
                        $data[':phone']=$_POST['phone'];
                        $update->execute($data);
                        $result=$update->fetch();
                        if($result){
                            $update= $db->prepare('update book set status=0 where bookid=:bookid');
                            $re[':bookid']=$_POST['bookid'];
                            if($update->execute($re)){
                                $updateUser= $db->prepare('update user set status=0 where phone=:phone');
                                $reUser[':phone']=$_POST['phone'];
                                if($updateUser->execute($reUser)){
                                    echo "<script> alert('还书成功!'); location.href='../html/searchBrowwer.php';</script>";
                                }else{
                                    echo "<script> alert('还书失败1!'); location.href='../html/huanshu.php'; </script>";
                                }
                            }else{
                                echo "<script> alert('还书失败2!'); location.href='../html/huanshu.php'; </script>";
                            }
                        }
                        if(empty($result)){
                            echo "<script> alert('归还日期不能为空!'); location.href='../html/huanshu.php'; </script>";
                        }

                    } catch (PDOException $e) {
                        echo 'Connection failed';
                    }
                    ?>
                </div>
            </form>
        </div>
        <div style="clear: both;"></div>
    </div>
</div>

</body>
</html>